<?php

class Paginator {

    var $paper_url = NULL; //duong dan cua trang do
    var $total_records = NULL; //tong so mau tin
    var $scroll_page = NULL; //so trang hien thi toi da
    var $per_page = NULL; //so mau tin(record) trong 1 trang
    var $current_page = NULL; //trang hien tai
    var $start = NULL;
    var $total_pages = NULL;
    var $page_link = NULL;
    var $previous_page = NULL;
    var $next_page = NULL;
    var $first_page = NULL;
    var $last_page = NULL;
    var $page_set = NULL;

    //--------------------tong so trang
    function totalPages($paper_url, $total_records, $scroll_page, $per_page, $current_page) {
	$this->paper_url = $paper_url;
	$this->total_records = $total_records;
	$this->scroll_page = $scroll_page;
	$this->per_page = $per_page;
	if (!is_numeric($current_page)) { //neu no khong la so thi cho trang hien  tai la 1
	    $this->current_page = 1;
	} else {//nguoc lai la trang khac
	    $this->current_page = $current_page;
	}
	if ($this->current_page == 1) {//phan trang dau tiên
	    $this->start = 0; //dung de chon vi tri bắt đầu trong câu lệnh limit trong sql
	} else {//các phân trang tiếp theo
	    $this->start = ($this->current_page - 1) * $this->per_page;
	    //giai thích : ví dụ có 20 records mỗi trang. khi bấm trang 5 thì điểm bắt đầu của 
	    //records trang 5 là (5-1)*20=80 (có nghĩa là gần record cuối trang 4) 
	}
	//ceil lay ra phan nguyen cua doan so float,vì tổng số trang = tổng số record/số record mỗi trang.

	$this->total_pages = ceil($this->total_records / $this->per_page);
    }

    //-------------------------------thiet lap link cho cac nut chuyen trang và thực hiện scroll các nút
    function pageLink($inactive_page_tag, $page_url_last) {
	//dùng scroll_page để rút gọn số nút hiển thị
	if ($this->total_pages <= $this->scroll_page) {
	    if ($this->total_records <= $this->per_page) {
		//trang dau tien se chay tu 1-> trang cuoi
		$loop_start = 1;
		$loop_finish = $this->total_pages;
	    } else {
		$loop_start = 1;
		$loop_finish = $this->total_pages;
	    }
	} else {//khi bấm tới giới hạn của scroll thì nó tự thêm nút trang tiếp theo
	    //vi du : cho scroll_page=3 
	    //neu trang hien tai chua=3
	    if ($this->current_page <= intval($this->scroll_page / 2) + 1) {//inval lay gia tri nguyen
		$loop_start = 1;
		$loop_finish = $this->scroll_page;
	    } else {//neu trang hien tai =3
		$loop_start = intval($this->current_page - ($this->scroll_page / 2)); //start=2;
		$loop_finish = intval($this->current_page + ($this->scroll_page / 2)); //finish=4;
		echo "<br>" . $loop_finish;
		if ($loop_finish > $this->total_pages)
		    $loop_finish = $this->total_pages;
	    }
	}
	//xuất các nút ra ngoài trình duyệt
	for ($i = $loop_start; $i <= $loop_finish; $i++) {
	    if ($i == $this->current_page) {//neu la trang hien tai
		$this->page_link.='<span ' . $inactive_page_tag . '>' . $i . '</span>';
	    } else {
		$this->page_link.='<span id="current_page"><a href="' . $this->paper_url . $i . $page_url_last . '">' . $i . '</a></span>';
	    }
	}
    }

    //-------------------nut trang truoc
    function pagePrevious($page_url_last) {

	//neu ban o trang 1 thi ban khong co trang truoc do=>phai bat dau tu page 2 moi co previous page
	if ($this->current_page > 1) {
	    $this->previous_page = '<a class="button paginator-btn" href="' . $this->paper_url . ($this->current_page - 1) . '"> << Previous</a>';
	}
    }

    //--------------------nut trang sau
    function pageNext($page_url_last) {

	//neu la trang cuoi thi khong co trang tiep theo
	if ($this->current_page < $this->total_pages) {

	    $this->next_page = '<a class="button paginator-btn" href="' . $this->paper_url . ($this->current_page + 1) . '"> Next >> </a>';
	}
    }

    //--------------------nut dau
    function firstPage($page_url_last) {
	if ($this->current_page > 1) {
	    $this->first_page = '<span><a href="' . $this->paper_url . "1" . $page_url_last . '">First Page</a></span>';
	}
    }

    //---------------------nut cuoi
    function lastPage($page_url_last) {
	if ($this->current_page < $this->total_pages) {
	    $this->last_page = '<span><a href="' . $this->paper_url . ($this->total_pages) . $page_url_last . '">Last Page</a></span>';
	}
    }

    //---------------------set cac gia tri trong function co trong class
    function page_set($paper_url, $total_records, $scroll_page, $per_page, $current_page, $inactive_page_tag, $page_url_last = '') {
	$this->totalPages($paper_url, $total_records, $scroll_page, $per_page, $current_page);
	$this->pageLink($inactive_page_tag, $page_url_last);
	$this->pagePrevious($page_url_last);
	$this->pageNext($page_url_last);
	$this->firstPage($page_url_last);
	$this->lastPage($page_url_last);
    }

}

